package com.jy.api.service.impl;

import com.jy.api.dao.StoreIpAddressDao;
import com.jy.api.domain.StoreIpAddress;
import com.jy.api.service.StoreIpAddressService;
import com.jy.api.signUpReq.StoreIpReq;
import com.jy.api.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xiaohua on 2019/4/2.
 */
@Service
public class StoreIpAddressServiceImpl implements StoreIpAddressService{
    @Autowired
    private StoreIpAddressDao dao;

    @Override
    public List<StoreIpAddress> findAllIp(String storeId) {
        List<StoreIpAddress> address = dao.findAllByStoreIdAndEnabled(storeId,true);
        return address;

    }



    @Override
    @Transactional
    public Result addIp(StoreIpReq req) {
        StoreIpAddress address;
        if(req.getId()==0){
            address =new StoreIpAddress(req);
       }else {
            address=dao.findOne(req.getId());
            BeanUtils.copyProperties(req,address);
       }
        dao.save(address);

        return new Result(200,"IP地址添加成功");
    }

    @Override
    @Transactional
    public Result deleteIp(int id) {
       StoreIpAddress address = dao.findOne(id);
       address.setEnabled(false);
       dao.save(address);
       return new Result(200,"IP地址刪除成功");
    }
}
